package com.bus.generateData;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.*;

public class BusBatchData {
    public static void main(String[] args) throws Exception {
        // 公交车、乘客数量、时间、站台

        String[] buss = {"1路公交车", "2路公交车", "3路公交车", "5路公交车", "6路公交车", "7路公交车", "9路公交车", "10路公交车", "606路公交车", "808路公交车"};

        Map<String, String[]> stationMap = new HashMap<>();
        stationMap.put("1路公交车", new String[]{"下元", "千峰路口", "理工大学", "迎泽桥西", "迎泽桥东", "省人大", "新建路口", "大南门", "青年路口", "并州饭店", "广场", "火车站(建设北路北仓大厦)"});
        stationMap.put("2路公交车", new String[]{"向阳店", "北下温", "北下温东招呼站", "新兰路大同路口", "2路招呼站", "兴安化工厂", "南寨村", "南寨村新区", "新华化工厂", "迎新北三巷新兰路口", "临时站", "南固碾", "迎新南二巷西口", "下兰村", "机动处", "结构厂", "新村", "赵庄", "赵庄南", "钢企公司", "古城", "林场", "五十五中", "太钢程南小区", "程家村", "新建路胜利街口", "北大街新建路口", "胜利桥东"});
        stationMap.put("3路公交车", new String[]{"长风停车场", "晋祠路新庄北街口", "省直机关党校", "3路招呼站", "市人大", "财大北校", "双塔西街新建南路口", "公交集团公司", "体育馆", "山大一院", "大南门", "青年路口", "并州饭店", "五一广场北", "五一路桥头街口", "五一路小学", "山大二院", "省军区", "小东门街新开南巷口", "迎春街", "北河湾路南口", "东中环凯旋街口(肿瘤医院)"});
        stationMap.put("5路公交车", new String[]{"下元", "和平南路小井峪街口", "太纺宿舍西", "和平南路南内环西街口", "和平公园", "沙沟", "省建六公司", "义井北", "和平南路长风街口", "义井", "十六中", "西峪东街", "晋祠路西峪东街口", "南堰北", "南堰", "南堰南", "南中环晋祠路口", "晋阳大道南中环口", "晋阳大道滨湖街口(董茹)", "金胜", "一电厂宿舍", "金胜村", "化肥厂", "化肥小区", "晋阳大道蒙山街口"});
        stationMap.put("6路公交车", new String[]{"小东流", "和平北路北中环街口", "晋机", "西宫", "千峰北路兴华街口", "千峰北路彭西二巷口", "千峰北路彭西四巷口", "漪汾街千峰北路口", "省心血管病医院(漪汾小区)", "漪汾桥西", "桃园北路府西街口", "桃园北路桃园三巷口", "桃园北路桃园四巷口", "桃园北路水西关南街口", "省人大", "新建路口", "大南门", "青年路口", "并州饭店", "广场", "火车站(迎泽大街国防宾馆)"});
        stationMap.put("7路公交车", new String[]{"铁建公司办公楼(河龙湾双孔桥)", "西局二中", "桃杏村", "菜市场", "五四街", "白矿俱乐部", "五三街", "解放街", "红楼站", "南坑", "九院", "官地广场"});
        stationMap.put("9路公交车", new String[]{"火车站(东安路)", "建设北路五龙口街口", "建设北路白龙庙街口", "建设北路大东关街口", "建设北路小学", "迎春街", "北河湾路南口", "招呼站", "剪子弯东口", "轻工学院", "下庄", "杨家峪村", "杨家峪"});
        stationMap.put("10路公交车", new String[]{"小东流", "和平北路北中环街口", "晋机", "西宫", "兴华街千峰北路口", "兴华街漪兴路口", "兴华街九丰路口", "胜利桥西", "胜利桥东(北沙河东侧)", "新建路北大街口", "柳溪街口", "旱西门", "府西街新建路口", "府西街半坡东街口", "府西街三桥街口", "山西省眼科医院", "柳北", "柳巷", "柳南", "青年路口", "并州饭店", "广场", "火车站(国防宾馆)"});
        stationMap.put("606路公交车", new String[]{"长风停车场", "长风西街千峰南路口", "和平南路长风西街口", "义井北", "省建六公司", "沙沟", "和平公园", "南内环千峰南路口", "南内环大王路口", "南内环桥西", "市人大", "财大北校", "南内环街长治路口", "大营盘", "省贸易学校(南内环街东岗路口)", "建设南路南内环街口", "建设南路双塔寺街口", "建设南路朝阳街口", "火车站(火车站南建设南路东侧)"});
        stationMap.put("808路公交车", new String[]{"电车公司", "城市学院", "胜利桥西", "胜利桥东", "金刚堰路北大街口", "金刚堰路柳溪街口", "金刚堰路旱西关街口", "桃园北路府西街口", "桃园北路桃园三巷口", "桃园北路桃园四巷口", "十五中", "水西门(新建路)", "新建路口", "大南门(迎泽大街)", "青年路口", "广场", "并州北路青年东街口", "并州北路并州东街口", "山西报社(双塔寺街)", "省人民医院", "建设南路双塔寺街口", "建设南路南内环街口", "建设南路南十方街口", "建设南路狄村街口", "建设南路亲贤北街口", "长风街建设南路口"});

        //下一个站台index
        Map<String, Integer> nextStationIndexMap = new HashMap<>();

        //公交车线路方向（true为正方向，否则反方向）
        Map<String, Boolean> busDirectionMap = new HashMap<>();

        String path = "C:\\Users\\吉日木图\\Desktop\\公交车系统\\data.txt";
        FileWriter fw = new FileWriter(path);
        BufferedWriter bw = new BufferedWriter(fw);

        //模拟当前时间，起始时间为早上六点
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        //乘客数量
        int passengersNum = 0;

        while (true) {

            StringBuffer info = new StringBuffer();
            //随机数
            Random r = new Random();

            String bus = buss[9];

            info.append(bus).append("|");

            calendar.add(Calendar.MINUTE, r.nextInt(3) + 3);
            calendar.set(Calendar.SECOND, r.nextInt(60));
            Date date = calendar.getTime(); //第一次执行定时任务的时间

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = sdf.format(date);

            //获取当前时间小时
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            System.out.println(hour);

            //早晚高峰期：7~9   17~19
            if ((7 <= hour && hour <= 9) || (17 <= hour && hour <= 19)) {
                //早晚高峰期20~50
                passengersNum = r.nextInt(20) + 30;
            } else {
                //平时乘客数量0~20
                passengersNum = r.nextInt(20);
            }

            info.append(passengersNum).append("|");

            info.append(now).append("|");

            //获取站台

            String[] stations = stationMap.get(bus);

            int stationIndex = 0;

            if (nextStationIndexMap.containsKey(bus)) {
                stationIndex = nextStationIndexMap.get(bus);
            }

            //站台
            info.append(stations[stationIndex]);

            boolean busDirection = true;

            if (busDirectionMap.containsKey(bus)) {
                busDirection = busDirectionMap.get(bus);
            } else {
                busDirectionMap.put(bus, true);
            }

            if (busDirection) {
                //到终点站
                if (stationIndex + 1 == stations.length) {
                    nextStationIndexMap.put(bus, stationIndex - 1);
                    busDirectionMap.put(bus, false);
                } else {
                    nextStationIndexMap.put(bus, stationIndex + 1);
                }
            } else {
                //到始发站
                if (stationIndex - 1 == -1) {
                    nextStationIndexMap.put(bus, stationIndex + 1);
                    busDirectionMap.put(bus, true);
                } else {
                    nextStationIndexMap.put(bus, stationIndex - 1);
                }
            }
            bw.write(info.toString() + "\n");
            bw.flush();

            //当前时间大于22点，公交车就停了
            if (hour >= 22 && (stationIndex == 0 || stationIndex == stations.length - 1)) {
                break;
            }

            //每隔0~500毫秒生成一条数据，模拟点击节目，要不然数据生成的太快
            //Thread.sleep(r.nextInt(500));
        }
    }
}
